package com.bank.entity;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Card {
    private String cardID;
    private String curID;
    private Date openDate;
    private double openMoney;
    private double balance;
    private String password;
    private String isReportLoss;
    private int customerID;
    private int savingID;

    // Getters and Setters
    public String getCardID() {
        return cardID;
    }

    public void setCardID(String cardID) {
        this.cardID = cardID;
    }

    public String getCurID() {
        return curID;
    }

    public void setCurID(String curID) {
        this.curID = curID;
    }

    public Date getOpenDate() {
        return openDate;
    }

    public void setOpenDate(Date openDate) {
        this.openDate = openDate;
    }

    public double getOpenMoney() {
        return openMoney;
    }

    public void setOpenMoney(double openMoney) {
        this.openMoney = openMoney;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getIsReportLoss() {
        return isReportLoss;
    }

    public void setIsReportLoss(String reportLoss) {
        this.isReportLoss = reportLoss;
    }

    public int getCustomerID() {
        return customerID;
    }

    public void setCustomerID(int customerID) {
        this.customerID = customerID;
    }

    public int getSavingID() {
        return savingID;
    }

    public void setSavingID(int savingID) {
        this.savingID = savingID;
    }

    // ====== 实用方法 ======

    /**
     * 检查卡片是否挂失
     * @return true 如果卡片已挂失，否则 false
     */
    public boolean isReportedLoss() {
        return "是".equals(this.isReportLoss);
    }

    /**
     * 检查输入的密码是否正确
     * @param inputPassword 用户输入的密码
     * @return true 如果密码匹配，否则 false
     */
    public boolean checkPassword(String inputPassword) {
        return this.password.equals(inputPassword);
    }

    /**
     * 获取格式化后的余额字符串
     * @return 格式化后的余额（如 "¥1000.00"）
     */
    public String getFormattedBalance() {
        return String.format("¥%.2f", this.balance);
    }

    /**
     * 获取存款类型名称
     * @return 存款类型名称（如 "活期"、"整存整取"）
     */
    public String getSavingTypeName() {
        DepositType type = DepositType.getById(this.savingID);
        return type != null ? type.getSavingName() : "未知类型";
    }

    /**
     * 获取开户日期字符串（格式为 yyyy-MM-dd）
     * @return 格式化后的开户日期字符串
     */
    public String getOpenDateString() {
        if (this.openDate == null) {
            return "未知日期";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(this.openDate);
    }

    /**
     * 存款操作
     * @param amount 存款金额
     */
    public void deposit(double amount) {
        if (amount > 0) {
            this.balance += amount;
        }
    }

    /**
     * 取款操作
     * @param amount 取款金额
     * @return true 如果取款成功，false 如果余额不足
     */
    public boolean withdraw(double amount) {
        if (amount > 0 && this.balance >= amount) {
            this.balance -= amount;
            return true;
        }
        return false;
    }

    /**
     * 获取卡片摘要信息
     * @return 卡片摘要信息
     */
    public String getSummary() {
        return String.format("卡号: %s | 余额: %s | 类型: %s",
                cardID, getFormattedBalance(), getSavingTypeName());
    }

    @Override
    public String toString() {
        return "Card{" +
                "cardID='" + cardID + '\'' +
                ", balance=" + getFormattedBalance() +
                ", savingType=" + getSavingTypeName() +
                ", openDate=" + getOpenDateString() +
                '}';
    }
}